export default {
	parseTime(time, cFormat) {
		if (arguments.length === 0 || time === null) {
			return null
		}
		const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
		let date;
		if (typeof time === 'object') {
			date = time;
		} else {
			if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) time = parseInt(time);
			if ((typeof time === 'number') && (time.toString().length === 10)) time = time * 1000;
			date = new Date(time);
		}
		const formatObj = {
			y: date.getFullYear(),
			m: date.getMonth() + 1,
			d: date.getDate(),
			h: date.getHours(),
			i: date.getMinutes(),
			s: date.getSeconds(),
			a: date.getDay()
		}
		const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
			let value = formatObj[key]
			if (key === 'a') {
				return ['日', '一', '二', '三', '四', '五', '六'][value]
			}
			if (result.length > 0 && value < 10) {
				value = '0' + value
			}
			return value || 0
		})
		return time_str;
	},
	isWithinOneMinute(timestamp1, timestamp2) {
		var date1 = new Date(timestamp1 * 1000);
		var date2 = new Date(timestamp2 * 1000);
		var timeDiff = Math.abs(date1.getTime() - date2.getTime());
		var diffInMinutes = timeDiff / 1000 / 60;
		return diffInMinutes <= 2;
	},
	isDy7Day(timestamp) {
		// 创建一个 Date 对象，单位为毫秒，所以时间戳要乘以 1000  
		var date = new Date(timestamp * 1000);
		// getDay 方法返回的是一个 0（代表周日） 到 6（代表周六）的整数  
		var dayOfWeek = date.getDay();
		// 获取当前日期  
		var now = new Date();
		// 获取当前周的第一天（周日）  
		var firstDayOfWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - now.getDay());
		// 判断给定时间戳是否在当前周内  
		return date >= firstDayOfWeek && date < firstDayOfWeek.setDate(firstDayOfWeek.getDate() + 7);
	},
	//聊天时间显示
	timeFliter(timer) {
		let timer2 = timer
		let sendTime = timer2
		let timeData = new Date(timer2)
		let nowData = new Date().getTime() //获取现在的时间
		let str = '';
		let yesterday = new Date(new Date().toLocaleDateString()).getTime() - 1000 //获取昨天凌晨23：59时间
		if ((yesterday - sendTime) > 31536000000) { //年-月-日 
			str = new Date(timeData).getFullYear() + '-' + (new Date(timeData).getMonth() + 1) + '-' + new Date(
				timeData).getDate()
		} else if ((yesterday - sendTime) > 86400000 && (yesterday - sendTime) < 31536000000) { //月 日 时分
			str = (new Date(timeData).getMonth() + 1) + '-' + new Date(timeData).getDate() + ' ' + new Date(
				timeData).getHours() + ':' + new Date(timeData).getMinutes()
		} else if ((yesterday - sendTime) < 86400000 && (yesterday - sendTime) > 0) { //月 日 时分
			str = (new Date(timeData).getMonth() + 1) + '-' + new Date(timeData).getDate() + ' ' + new Date(
				timeData).getHours() + ':' + new Date(timeData).getMinutes()
		} else { //当天
			let Minutes = new Date(timeData).getMinutes()
			let MinutesStr = ''
			if (Minutes > 9) {
				MinutesStr = Minutes
			} else {
				MinutesStr = '0' + Minutes
			}
			str = new Date(timeData).getHours() + ':' + MinutesStr
		}
		return str
	},
	getDayOrYesterday(timestamp) {
		let timestamp2 = timestamp * 1000
		let time_str = ''
		if (new Date(timestamp2).getDate() === new Date().getDate()) {
			time_str = 1;
		} else if (new Date(timestamp2).getDate() === (new Date().getDate() - 1)) {
			time_str = 0;
		}

		// else if (new Date(date).getDate() === (new Date().getDate() + 1)) {
		// 	time_str = "明天";
		// } else if (new Date(date).getDate() < new Date().getDate()) {
		// 	time_str = "过去";
		// }
		return time_str;
	},
	getDayOfWeek(timestamp) {
		// 创建一个 Date 对象，单位为毫秒，所以时间戳要乘以 1000  
		var date = new Date(timestamp * 1000);
		// getDay 方法返回的是一个 0（代表周日） 到 6（代表周六）的整数  
		var day = date.getDay();
		// 创建一个数组来表示星期几  
		var days = [this.$t('date.7'), this.$t('date.1'), this.$t('date.2'), this.$t('date.3'), this.$t('date.4'), this
			.$t('date.5'), this.$t('date.6')
		];

		// 返回对应的星期几  
		return days[day];
	},
}